/**
 * Copyright 2011-2012 OpenAjax Alliance
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/* ---------------------------------------------------------------- */
/*                  Initialize namespace properties                 */
/* ---------------------------------------------------------------- */

OAA_CI.tables = OAA_CI.tables || {};

var properties_tabpanel;

OAA_CI.tables.treeView   = new OAA_CI.SidebarView('ID_TREECHILDREN_TABLES_TREE');
OAA_CI.tables.listView   = new OAA_CI.SidebarView('ID_TREECHILDREN_TABLES_LIST');

OAA_CI.tables.views = [OAA_CI.tables.treeView, OAA_CI.tables.listView];

OAA_CI.tables.hideMeter = function () { OAA_CI.tables.meter.hidden = true; };
OAA_CI.tables.showMeter = function () { OAA_CI.tables.meter.hidden = false; };

OAA_CI.sidebar = OAA_CI.tables;

/**
 * getDisplayProperties
 *
 * @desc Add getDisplayProperties method to cache elements;
 *       called by populateTreeAs... functions.
 *
 * @return List of property values to be displayed in the sidebar
 */

OpenAjax.a11y.cache.TableElement.prototype.getDisplayProperties = function () {
  return [this.dom_element.tag_name, this.toString(), this.dom_element.getAccessibility()];
};

OpenAjax.a11y.cache.CaptionElement.prototype.getDisplayProperties = function () {
  return [this.dom_element.tag_name, this.toString(), this.dom_element.getAccessibility()];
};

OpenAjax.a11y.cache.THeadElement.prototype.getDisplayProperties = function () {
  return [this.dom_element.tag_name, this.toString(), this.dom_element.getAccessibility()];
};

OpenAjax.a11y.cache.TBodyElement.prototype.getDisplayProperties = function () {
  return [this.dom_element.tag_name, this.toString(), this.dom_element.getAccessibility()];
};

OpenAjax.a11y.cache.TableRowElement.prototype.getDisplayProperties = function () {
  return [this.dom_element.tag_name, this.toString(), this.dom_element.getAccessibility()];
};

OpenAjax.a11y.cache.TableCellElement.prototype.getDisplayProperties = function () {
  return [this.dom_element.tag_name, this.toString(), this.dom_element.getAccessibility()];
};

OpenAjax.a11y.ResultRule.prototype.getDisplayProperties = function () {
  return [this.rule.getTitle(), this.nodes_passed.length, this.nodes_violations.length, this.nodes_recommendations.length, this.nodes_warnings.length, this.nodes_informational.length, this.nodes_hidden.length, this.nodes_na.length];
};

OpenAjax.a11y.ResultNode.prototype.getDisplayProperties = function () {
  return [this.getSeverity(), this.toString(),  this.getXPath()];
};

/**
 * getChildren
 *
 * @desc Add getChildren method to cache elements; called by
 *       populateTreeAsHierarchy function.
 *
 * @return List of child elements
 */

OpenAjax.a11y.cache.TableElement.prototype.getChildren = function () {
  return this.child_cache_elements;
};

OpenAjax.a11y.cache.CaptionElement.prototype.getChildren = function () {
  return [];
};

OpenAjax.a11y.cache.THeadElement.prototype.getChildren = function () {
  return this.child_cache_elements;
};

OpenAjax.a11y.cache.TBodyElement.prototype.getChildren = function () {
  return this.child_cache_elements;
};

OpenAjax.a11y.cache.TableRowElement.prototype.getChildren = function () {
  return this.child_cache_elements;
};

OpenAjax.a11y.cache.TableCellElement.prototype.getChildren = function () {
  return this.child_cache_elements;
};

/**
 * @function onLoad
 *
 * @memberOf OAA_CI.tables
 *
 * @desc Initialize sidebar and populate its default tabpanel.
 */

OAA_CI.tables.onLoad = function () {

  var config = {
   rules      : true,
   attributes : true,
   properties : true,
   style      : false,
   events     : true
  };

  var pt = new OAA_CI.Properties.PropertiesTabPanel("ID_PROPERTIES", config);
  OAA_CI.tables.treeView.addProperties(pt);  
  OAA_CI.tables.listView.addProperties(pt);

  OAA_CI.tables.meter = document.getElementById('ID_METER_TABLES');
  OAA_CI.tables.treeView.isInitialized = false;
  OAA_CI.tables.treeView.showCacheItems();

};

/**
 * @function treeView.showCacheItems
 *
 * @memberOf OAA_CI.tables
 *
 * @desc Respond to the Tree button in the sidebar's main tab panel.
 *       If the panel has not been initialized, populate it with image
 *       elements structured as a flat list. Update properties display
 *       and page highlighting based on selection state of panel.
 */

OAA_CI.tables.treeView.showCacheItems = function () {
  var view = OAA_CI.tables.treeView;
  if (!view.isInitialized) {
    
    // Save references
    view.tab = document.getElementById('ID_TAB_TABLES_TREE');
    view.tree = document.getElementById('ID_TREE_TABLES_TREE');
    view.cache = OAA_CI.ruleset.dom_cache.tables_cache;
 
    // Populate the tabpanel
    var child_elements = view.cache.child_cache_elements;
    OAA_CI.populateTreeAsHierarchy(child_elements, 'ID_TREECHILDREN_TABLES_TREE');
    view.isInitialized = true;
  }

  view.updateItemSelectionDisplay();
};

/**
 * @function listView.showCacheItems
 *
 * @memberOf OAA_CI.tables
 *
 * @desc Respond to the List button in the sidebar's main tab panel.
 *       If the panel has not been initialized, populate it with image
 *       elements structured as a flat list. Update properties display
 *       and page highlighting based on selection state of panel.
 */

OAA_CI.tables.listView.showCacheItems = function () {
  var view = OAA_CI.tables.listView;

  if (!view.isInitialized) {
    // Save references
    view.tab = document.getElementById('ID_TAB_TABLES_LIST');
    view.tree = document.getElementById('ID_TREE_TABLES_LIST');
    view.cache = OAA_CI.ruleset.dom_cache.tables_cache;

    // Populate the tabpanel
    var table_elements = view.cache.table_elements;
    OAA_CI.populateTreeAsHierarchy(table_elements, 'ID_TREECHILDREN_TABLES_LIST');
    view.isInitialized = true;
  }
 
  view.updateItemSelectionDisplay();
};


/**
 * @function onItemSelectionChange
 *
 * @memberOf OAA_CI.tables
 *
 * @desc Handle the onselect event from the XUL tree in the main tab panel
 *       by calling the F method.
 *
 * @param {Number} index - Specifies the view that triggered the onselect event.
 */

OAA_CI.tables.onItemSelectionChange = function (index) {

  var view = OAA_CI.tables.views[index];
  view.updateItemSelectionDisplay();

};


